﻿/* File Created: 三月 31, 2012 产品查询主界面 */
var g_type = null;
var gridList = null;
$(function () {
    $.getJSON(getComboxDataUrl, { AddEmpty: 1 }, function (json) {
        if (json) {
            g_type = $("#selCategoryId").scombo({
                zIndex: 10000,
                enableInput: true,
                multiSelect: false,
                inputCheck: true,
                filterConfig: { delay: 400, minChars: 0, matchCase: false, selectFirst: true, autoFill: false },
                emptyText: "",
                width: 60,
                enableOperation: false,
                onlyOneSelected: true,
                source: json
            });
            g_type.val(0);
        }
    });
    ts = new TBSerials();
    gridList = $("#list");
    if (gridList.getGridParam("url") == undefined) {
        url = getQueryUrl();
        loadData(url);
        AddColnumber();
    }
    else {
        gridList.setGridParam({ url: getListUrl, page: 1 }).trigger("reloadGrid");
    }
});
function getQueryUrl() {
    if (g_type != null && g_type.val() != "") {
        $("#hd_CategoryId").val(g_type.val());
    }

    var condition = escape(ts.ToSerials("queryDT"));
    var url = getUrl + "?d=" + condition;
    return url;
}
//加载数据
function loadData(url) {
    gridList.jqGrid({
        url: url, //调用返回数据的页面URL
        autowidth: true,
        forceFit: false,
        shrinkToFit: false,
        multiselect: false,
        colNames: ['id', '操作', '产品代码', '品种', "产品名称", "规格",],
        colModel: [
            { name: 'Id', sortable: true, index: 'id', align: 'left', hidden: true },
            { name: 'operate', sortable: false, index: 'operate', align: 'center', width: 150 },
            { name: 'Code', sortable: true, index: 'Code', align: 'left', width: 100 },
            { name: 'ProductCategory.Name', sortable: true, index: 'ProductCategory.Name', align: 'left', width: 100 },
            { name: 'Name', sortable: true, index: 'Name', align: 'left', width: 100 },
            { name: 'Specification', sortable: true, index: 'Specification', align: 'left', width: 100 }
           
        ], //列设置列表
        rownumWidth: 40,
        pager: $('#pager'), //页码控件
        gridComplete: function () {//表格载入完毕后执行的事件   
            var ids = gridList.getDataIDs();
            for (var i = 0; i < ids.length; i++) {
                if (i % 2 != 0) {
                    $("#" + ids[i]).addClass("even");
                }
                //更新图表状态
                var rowId = ids[i];
                var itemId = gridList.getCell(rowId, "Id");
                var itemStatus = gridList.getCell(rowId, "Status");
                operate = "";
                operate = "<input class='motion_text' type='button' value='修改' onclick='upInfo(\"" + itemId + "\")'/>";
                operate += "<input class='motion_text' type='button' value='删除' onclick='delInfo(\"" + itemId + "\")'/>";

                gridList.setRowData(rowId, { operate: operate });
            }
        },
        altRows: true,
        onPaging: function () {//分页事件
            if (gridList.getGridParam('rowNum') != _$_CurrRowNum) {
                gridList.setGridParam({ page: '1' });
                _$_CurrRowNum = gridList.getGridParam('rowNum');
            }
        },
        caption: ""//网格顶行标题名称
    });
}
//#endregion

function QueryInfo() {
    var url = getQueryUrl();
    gridList.setGridParam({ url: url, page: 1 }).trigger("reloadGrid");
}
function upInfo(id) {
    var tempUrl = editUrl + "?Id=" + id;
    dialog.opendlg("修改产品信息", tempUrl, 800, 500, true, true, '', true, { top: 50 });
}
function AddInfo() {
    dialog.opendlg("新增产品信息", editUrl, 800, 500, true, true, '', true, { top: 50 });
}
function delInfo(id) {
    ymPrompt.confirmInfo({
        message: "<div>确认删除所选择的记录吗?</div>", width: 300, height: 150,
        handler: function (tp) {
            if (tp == 'ok') {
                $.post(delUrl, { Id: id }, function (json) {
                    if (json.Text == "ok") {
                        $("#list").trigger("reloadGrid");
                        ymAlert("数据删除成功");
                    }
                    else {
                        ymAlert(json.Value);
                    }
                }, 'json');
            }
        }
    });
}